package Visitor;

import Connection.IPServer;
import Controllers.Application;
import Entities.Advantages.DecreaseBallSpeed;
import Entities.Advantages.DecreaseBarSize;
import Entities.Advantages.IAdvantage;
import Entities.Advantages.IncreaseBallSpeed;
import Entities.Advantages.IncreaseBarSize;
import Entities.Advantages.MissileAdvantage;
import Entities.Advantages.ThreeBallsAdvantage;
import Entities.Ball;
import Entities.Bar;
import Entities.BarSize;
import Entities.Boundary;
import Entities.Brick;
import Entities.Drawable;
import Entities.Hinder;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class GraphicPaintVisitor implements IVisitor {

    private Graphics g;
    private Image ballImage;
    private Image normalBarImage;
    private Image smallBarImage;
    private Image bigBarImage;
    private Image brick1;
    private Image brick2;
    private Image brick3;
    private Image brick4;
    private Image brick5;
    private Image brick6;
    private Image brick7;
    private Image brickInf;
    private Image missile;
    private Image increaseBarSizeImage;
    private Image threeBallsImage;
    private Image decreaseBallSpeedImage;
    private Image decreaseBarSizeImage;
    private Image increaseBallSpeedImage;

    public GraphicPaintVisitor(Application application, Graphics g) {
        try {
            IPServer iPServer = new IPServer(application);
            this.g = g;
            ballImage = ImageIO.read(new URL(iPServer.getAdress() + "/Images/bola.png"));
            normalBarImage = ImageIO.read(new URL(iPServer.getAdress() + "/Images/bar/barra.png"));
            smallBarImage = ImageIO.read(new URL(iPServer.getAdress() + "/Images/bar/barraPequena.png"));
            bigBarImage = ImageIO.read(new URL(iPServer.getAdress() + "/Images/bar/barraGrande.png"));
            brick1 = ImageIO.read(new URL(iPServer.getAdress() + "/Images/bricks/brick1.png"));
            brick2 = ImageIO.read(new URL(iPServer.getAdress() + "/Images/bricks/brick2.png"));
            brick3 = ImageIO.read(new URL(iPServer.getAdress() + "/Images/bricks/brick3.png"));
            brick4 = ImageIO.read(new URL(iPServer.getAdress() + "/Images/bricks/brick4.png"));
            brick5 = ImageIO.read(new URL(iPServer.getAdress() + "/Images/bricks/brick5.png"));
            brick6 = ImageIO.read(new URL(iPServer.getAdress() + "/Images/bricks/brick6.png"));
            brick7 = ImageIO.read(new URL(iPServer.getAdress() + "/Images/bricks/brick7.png"));
            brickInf = ImageIO.read(new URL(iPServer.getAdress() + "/Images/bricks/brickInf.png"));
            increaseBarSizeImage = ImageIO.read(new URL(iPServer.getAdress() + "/Images/advantages/increaseBarSize.png"));
            threeBallsImage = ImageIO.read(new URL(iPServer.getAdress() + "/Images/advantages/threeBalls.png"));
            decreaseBallSpeedImage = ImageIO.read(new URL(iPServer.getAdress() + "/Images/advantages/decreaseBallSpeed.png"));
            decreaseBarSizeImage = ImageIO.read(new URL(iPServer.getAdress() + "/Images/advantages/decreaseBarSize.png"));
            increaseBallSpeedImage = ImageIO.read(new URL(iPServer.getAdress() + "/Images/advantages/increaseBallSpeed.png"));
            missile = ImageIO.read(new URL(iPServer.getAdress() + "/Images/misil.png"));
        } catch (IOException ex) {
            Logger.getLogger(GraphicPaintVisitor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public Drawable visit(Bar bar) {
        synchronized (bar) {
            if (bar.getSize().equals(BarSize.SMALL)) {
                g.drawImage(smallBarImage, (int) bar.getX(), (int) bar.getY(), null);
            } else if (bar.getSize().equals(BarSize.NORMAL)) {
                g.drawImage(normalBarImage, (int) bar.getX(), (int) bar.getY(), null);
            } else if (bar.getSize().equals(BarSize.HIGH)) {
                g.drawImage(bigBarImage, (int) bar.getX(), (int) bar.getY(), null);
            }
        }
        return null;
    }

    @Override
    public Drawable visit(Ball ball) {
        if (ball.getDx() == 0 && ball.getDy() == 0) {
            g.setColor(Color.WHITE);
            g.drawString("Press SPACE", 220, 450);
            g.setColor(Color.BLACK);
        }
        g.drawImage(ballImage, (int) ball.getX(), (int) ball.getY(), null);
        return null;
    }

    @Override
    public Drawable visit(Boundary boundary) {
        g.setColor(Color.WHITE);
        g.drawRect((int) boundary.getX(), (int) boundary.getY(), (int) boundary.getWidth(), (int) boundary.getHeight());
        g.setColor(Color.BLACK);
        return null;
    }

    @Override
    public Drawable visit(IAdvantage advantage) {
        return null;
    }

    @Override
    public Drawable visit(Brick brick) {
        switch (brick.getResistance()) {
            case 0: {
                g.drawImage(brick1, (int) brick.getX(), (int) brick.getY(), null);
                break;
            }
            case 1: {
                g.drawImage(brick2, (int) brick.getX(), (int) brick.getY(), null);
                break;
            }
            case 2: {
                g.drawImage(brick3, (int) brick.getX(), (int) brick.getY(), null);
                break;
            }
            case 3: {
                g.drawImage(brick4, (int) brick.getX(), (int) brick.getY(), null);
                break;
            }
            case 4: {
                g.drawImage(brick5, (int) brick.getX(), (int) brick.getY(), null);
                break;
            }
            case 5: {
                g.drawImage(brick6, (int) brick.getX(), (int) brick.getY(), null);
                break;
            }
            case 6: {
                g.drawImage(brick7, (int) brick.getX(), (int) brick.getY(), null);
                break;
            }
            default: {
                g.drawImage(brickInf, (int) brick.getX(), (int) brick.getY(), null);
                break;
            }
        }
        return null;
    }

    @Override
    public Drawable visit(Drawable drawable) {
        return null;
    }

    @Override
    public Drawable visit(Hinder hinder) {
        return null;
    }

    @Override
    public Drawable visit(DecreaseBarSize decreaseBarSize) {
        g.drawImage(decreaseBarSizeImage, (int) decreaseBarSize.getX(), (int) decreaseBarSize.getY(), null);
        return null;
    }

    @Override
    public Drawable visit(IncreaseBarSize increaseBarSize) {
        g.drawImage(increaseBarSizeImage, (int) increaseBarSize.getX(), (int) increaseBarSize.getY(), null);
        return null;
    }

    @Override
    public Drawable visit(DecreaseBallSpeed drecreaseBallSpeed) {
        g.drawImage(decreaseBallSpeedImage, (int) drecreaseBallSpeed.getX(), (int) drecreaseBallSpeed.getY(), null);
        return null;
    }

    @Override
    public Drawable visit(IncreaseBallSpeed increaseBallSpeed) {
        g.drawImage(increaseBallSpeedImage, (int) increaseBallSpeed.getX(), (int) increaseBallSpeed.getY(), null);
        return null;
    }

    @Override
    public Drawable visit(MissileAdvantage missileAdvantage) {
        //TODO: añadir foto cuando se implemente la ventaja
        g.setColor(Color.magenta);
        g.fillRect((int) missileAdvantage.getX(), (int) missileAdvantage.getY(), (int) missileAdvantage.getWidth(), (int) missileAdvantage.getHeight());
        g.setColor(Color.black);
        return null;
    }

    @Override
    public Drawable visit(ThreeBallsAdvantage threeBallsAdvantage) {
        g.drawImage(threeBallsImage, (int) threeBallsAdvantage.getX(), (int) threeBallsAdvantage.getY(), null);
        return null;
    }
}
